﻿using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Configuration;
using System.Web.Security;
using System.Data.Odbc;
using System.Collections.Generic;
using System.Collections;
using System.Threading;
using System.Text;
using System.Reflection;

namespace Core
{
    #region AccountImpl类
    public class AccountImpl
    {
        static AccountImpl m_Instance = new AccountImpl();

        public static AccountImpl Instance
        {
            get { return m_Instance; }
        }

        // 缓存中的用户信息的数量
#		if DEBUG
        const int MAX_CACHE_COUNT = 20000;
#		else
		const int MAX_CACHE_COUNT = 20000;
#		endif

        LinkedList<AccountInfo> m_List = new LinkedList<AccountInfo>();

        Hashtable m_UserInfoCache = new Hashtable();
        Hashtable m_UserInfoCacheByID = new Hashtable();

        object m_Lock = new object();

        IAccountStorage m_IAccountStorage = null;

        private AccountImpl()
        {
        }

        public void Init()
        {
            Configuration config = WebConfigurationManager.OpenWebConfiguration(
                HttpContext.Current.Request.ApplicationPath == "/" ? "/Lesktop" : HttpContext.Current.Request.ApplicationPath + "/Lesktop"
            );

            string accStorage = config.AppSettings.Settings["AccountStorageImpl"].Value;
            String[] accStorageInfo = accStorage.Split(new char[] { ' ' });
            Type type = Assembly.Load(accStorageInfo[0]).GetType(accStorageInfo[1]);
            ConstructorInfo ctor = type.GetConstructor(new Type[] { });
            m_IAccountStorage = ctor.Invoke(new object[] { }) as IAccountStorage;
        }

        public AccountInfo RefreshUserInfo(string userName)
        {
            string key = userName.ToUpper();

            DataRow userInfo = m_IAccountStorage.GetAccountInfo(userName);

            if (userInfo != null)
            {
                List<FriendInfo> friends = new List<FriendInfo>(), managers = new List<FriendInfo>();
                FriendInfo creator = null;
                foreach (DataRow row in m_IAccountStorage.GetFriends(userName))
                {
                    string name = row["Name"] as string;
                    DateTime renewTime = (DateTime)row["RenewTime"];
                    FriendInfo fi = new FriendInfo(name, renewTime, Convert.ToInt64(row["Relationship"]), Convert.ToInt64(row["Type"]));
                    friends.Add(fi);
                    switch (Convert.ToInt64(row["Relationship"]))
                    {
                        case 2:
                            managers.Add(fi);
                            break;
                        case 3:
                            managers.Add(fi);
                            creator = fi;
                            break;
                    }
                }

                /*获取待办事项和未读邮件的条数*/
                Configuration config = WebConfigurationManager.OpenWebConfiguration(
                 HttpContext.Current.Request.ApplicationPath == "/" ? "/Lesktop" : HttpContext.Current.Request.ApplicationPath + "/Lesktop"
             );

                string webServiceUrl = config.AppSettings.Settings["WebServiceKey"].Value.ToString();

　　              /*DynWebService dws = new DynWebService(webServiceUrl);                
                string strWaitToDo = dws.WaitToDoCount(userInfo["Name"].ToString());
                string strUnReadEmail = dws.UnReadEmailCount(userInfo["Name"].ToString());
                string strWaitToDoUrl = dws.GoMoreWaitToDo(userInfo["Name"].ToString());
                string strUnReadEmailUrl = dws.GoMoreEmail(userInfo["Name"].ToString());
                */
                object[] objUserName = { "System.String:" + userInfo["Name"].ToString() };
                object returnWaitToDo = DynamicWebServices.InvokeWebService(webServiceUrl, "AosWs", "WaitToDoCount", objUserName);
                object returnUnReadEmail = DynamicWebServices.InvokeWebService(webServiceUrl, "AosWs", "UnReadEmailCount", objUserName);
                object returnUnReadEmailUrl = DynamicWebServices.InvokeWebService(webServiceUrl, "AosWs", "GoMoreEmail", objUserName);
                object returnWaitToDoUrl = DynamicWebServices.InvokeWebService(webServiceUrl, "AosWs", "GoMoreWaitToDo", objUserName);

                string strWaitToDo = returnWaitToDo.ToString();
                string strUnReadEmail = returnUnReadEmail.ToString();
                string strWaitToDoUrl = returnWaitToDoUrl.ToString();
                string strUnReadEmailUrl = returnUnReadEmailUrl.ToString();

                if (m_UserInfoCache.ContainsKey(key))
                {
                    AccountInfo info = m_UserInfoCache[key] as AccountInfo;
                    info.Reset(
                        userInfo["Name"] as string,
                        userInfo["NickName"] as string,
                        Convert.ToInt64(userInfo["Key"]),
                        Convert.ToInt64(userInfo["Type"]),
                        m_IAccountStorage.GetUserRoles(userName),
                        friends.ToArray(),
                        Convert.ToInt64(userInfo["Type"]) == 1 ? managers.ToArray() : null,
                        creator,
                        userInfo["EMail"] as String,
                        userInfo["InviteCode"] as String,
                        Convert.ToInt64(userInfo["AcceptStrangerIM"]) != 0,
                        Convert.ToInt64(userInfo["MsgFileLimit"]),
                        Convert.ToInt64(userInfo["MsgImageLimit"]),
                        Convert.ToInt64(userInfo["DiskSize"]),
                        Convert.ToInt64(userInfo["IsTemp"]),
                        ((DateTime)userInfo["RegisterTime"]),
                        userInfo["HomePage"] as string,
                        userInfo["Password"] as string,
                        userInfo,
                        strWaitToDo,
                        strUnReadEmail,
                        strWaitToDoUrl,
                        strUnReadEmailUrl
                    );
                    return info;
                }
                else
                {
                    AccountInfo info = new AccountInfo(
                        userInfo["Name"] as string,
                        userInfo["NickName"] as string,
                        Convert.ToInt64(userInfo["Key"]),
                        Convert.ToInt64(userInfo["Type"]),
                        m_IAccountStorage.GetUserRoles(userName),
                        friends.ToArray(),
                        Convert.ToInt64(userInfo["Type"]) == 1 ? managers.ToArray() : null,
                        creator,
                        userInfo["EMail"] as String,
                        userInfo["InviteCode"] as String,
                        Convert.ToInt64(userInfo["AcceptStrangerIM"]) != 0,
                        Convert.ToInt64(userInfo["MsgFileLimit"]),
                        Convert.ToInt64(userInfo["MsgImageLimit"]),
                        Convert.ToInt64(userInfo["DiskSize"]),
                        Convert.ToInt64(userInfo["IsTemp"]),
                        ((DateTime)userInfo["RegisterTime"]),
                        userInfo["HomePage"] as string,
                        userInfo["Password"] as string,
                        userInfo,
                        strWaitToDo,
                        strUnReadEmail,
                        strWaitToDoUrl,
                        strUnReadEmailUrl
                    );

                    if (m_List.Count >= MAX_CACHE_COUNT)
                    {
                        AccountInfo removeInfo = m_List.First.Value;
                        m_UserInfoCache.Remove(removeInfo.Name.ToUpper());
                        m_UserInfoCacheByID.Remove(removeInfo.ID);
                        m_List.RemoveFirst();
                    }

                    m_UserInfoCache[key] = info;
                    m_UserInfoCacheByID[info.ID] = info;
                    m_List.AddLast(info.ListNode);

                    return info;
                }
            }
            else
            {
                return null;
            }
        }

        public DataRowCollection GetAllUsers()
        {
            return m_IAccountStorage.GetAllUsers();
        }

        public DataRowCollection GetAllGroups()
        {
            return m_IAccountStorage.GetAllGroups();
        }

        private AccountInfo RefreshUserInfo(Int64 id)
        {
            DataRow userInfo = m_IAccountStorage.GetAccountInfo(id);

            if (userInfo != null)
            {
                string userName = userInfo["Name"] as string;

                List<FriendInfo> friends = new List<FriendInfo>(), managers = new List<FriendInfo>();
                FriendInfo creator = null;
                foreach (DataRow row in m_IAccountStorage.GetFriends(userName))
                {
                    string name = row["Name"] as string;
                    DateTime renewTime = (DateTime)row["RenewTime"];
                    FriendInfo fi = new FriendInfo(name, renewTime, Convert.ToInt64(row["Relationship"]), Convert.ToInt64(row["Type"]));
                    friends.Add(fi);
                    switch (Convert.ToInt64(row["Relationship"]))
                    {
                        case 2:
                            managers.Add(fi);
                            break;
                        case 3:
                            managers.Add(fi);
                            creator = fi;
                            break;
                    }
                }

                /*获取待办事项和未读邮件的条数*/
                Configuration config = WebConfigurationManager.OpenWebConfiguration(
                HttpContext.Current.Request.ApplicationPath == "/" ? "/Lesktop" : HttpContext.Current.Request.ApplicationPath + "/Lesktop"
            );

                string webServiceUrl = config.AppSettings.Settings["WebServiceKey"].Value.ToString();
                //string webServiceUrl = ConfigurationManager.AppSettings["WebServiceKey "].ToString();

                /*
                DynWebService dws = new DynWebService(webServiceUrl);

                string strWaitToDo = dws.WaitToDoCount(userInfo["Name"].ToString());
                string strUnReadEmail = dws.UnReadEmailCount(userInfo["Name"].ToString());
                string strWaitToDoUrl = dws.GoMoreWaitToDo(userInfo["Name"].ToString());
                string strUnReadEmailUrl = dws.GoMoreEmail(userInfo["Name"].ToString());
                */
                object[] objUserName = { "System.String:" + userInfo["Name"].ToString() };
                object returnWaitToDo = DynamicWebServices.InvokeWebService(webServiceUrl, "AosWs", "WaitToDoCount", objUserName);
                object returnUnReadEmail = DynamicWebServices.InvokeWebService(webServiceUrl, "AosWs", "UnReadEmailCount", objUserName);
                object returnUnReadEmailUrl = DynamicWebServices.InvokeWebService(webServiceUrl, "AosWs", "GoMoreEmail", objUserName);
                object returnWaitToDoUrl = DynamicWebServices.InvokeWebService(webServiceUrl, "AosWs", "GoMoreWaitToDo", objUserName);

                string strWaitToDo = returnWaitToDo.ToString();
                string strUnReadEmail = returnUnReadEmail.ToString();
                string strWaitToDoUrl = returnWaitToDoUrl.ToString();
                string strUnReadEmailUrl = returnUnReadEmailUrl.ToString();

                if (m_UserInfoCache.ContainsKey(userName.ToUpper()))
                {
                    AccountInfo info = m_UserInfoCache[userName.ToUpper()] as AccountInfo;
                    info.Reset(
                        userInfo["Name"] as string,
                        userInfo["NickName"] as string,
                        Convert.ToInt64(userInfo["Key"]),
                        Convert.ToInt64(userInfo["Type"]),
                        m_IAccountStorage.GetUserRoles(userName),
                        friends.ToArray(),
                        Convert.ToInt64(userInfo["Type"]) == 1 ? managers.ToArray() : null,
                        creator,
                        userInfo["EMail"] as String,
                        userInfo["InviteCode"] as String,
                        Convert.ToInt64(userInfo["AcceptStrangerIM"]) != 0,
                        Convert.ToInt64(userInfo["MsgFileLimit"]),
                        Convert.ToInt64(userInfo["MsgImageLimit"]),
                        Convert.ToInt64(userInfo["DiskSize"]),
                        Convert.ToInt64(userInfo["IsTemp"]),
                        ((DateTime)userInfo["RegisterTime"]),
                        userInfo["HomePage"] as string,
                        userInfo["Password"] as string,
                        userInfo,
                        strWaitToDo,
                        strUnReadEmail,
                        strWaitToDoUrl,
                        strUnReadEmailUrl
                    );
                    return info;
                }
                else
                {
                    AccountInfo info = new AccountInfo(
                        userInfo["Name"] as string,
                        userInfo["NickName"] as string,
                        Convert.ToInt64(userInfo["Key"]),
                        Convert.ToInt64(userInfo["Type"]),
                        m_IAccountStorage.GetUserRoles(userName),
                        friends.ToArray(),
                        Convert.ToInt64(userInfo["Type"]) == 1 ? managers.ToArray() : null,
                        creator,
                        userInfo["EMail"] as String,
                        userInfo["InviteCode"] as String,
                        Convert.ToInt64(userInfo["AcceptStrangerIM"]) != 0,
                        Convert.ToInt64(userInfo["MsgFileLimit"]),
                        Convert.ToInt64(userInfo["MsgImageLimit"]),
                        Convert.ToInt64(userInfo["DiskSize"]),
                        Convert.ToInt64(userInfo["IsTemp"]),
                        ((DateTime)userInfo["RegisterTime"]),
                        userInfo["HomePage"] as string,
                        userInfo["Password"] as string,
                        userInfo,
                        strWaitToDo,
                        strUnReadEmail,
                        strWaitToDoUrl,
                        strUnReadEmailUrl
                    );

                    if (m_List.Count >= MAX_CACHE_COUNT)
                    {
                        AccountInfo removeInfo = m_List.First.Value;
                        m_UserInfoCache.Remove(removeInfo.Name.ToUpper());
                        m_UserInfoCacheByID.Remove(removeInfo.ID);
                        m_List.RemoveFirst();
                    }

                    m_UserInfoCache[userName.ToUpper()] = info;
                    m_UserInfoCacheByID[info.ID] = info;
                    m_List.AddLast(info.ListNode);

                    return info;
                }
            }
            else
            {
                return null;
            }
        }

        private String[] GetGroupManagers(string name)
        {
            return m_IAccountStorage.GetGroupManagers(name);
        }

        public bool Validate(string userId, string password)
        {
            lock (m_Lock)
            {
                return m_IAccountStorage.Validate(userId, password);
            }
        }

        public AccountInfo GetUserInfo(string user)
        {
            if (String.IsNullOrEmpty(user)) return null;
            lock (m_Lock)
            {
                string key = user.ToUpper();
                AccountInfo ai = null;
                if (m_UserInfoCache.ContainsKey(key))
                {
                    ai = m_UserInfoCache[key] as AccountInfo;
                    m_List.Remove(ai.ListNode);
                    m_List.AddLast(ai.ListNode);
                }
                else
                {
                    ai = RefreshUserInfo(user);
                }
                return ai;
            }
        }

        public AccountInfo GetUserInfo(Int64 userId)
        {
            lock (m_Lock)
            {
                AccountInfo ai = null;
                if (m_UserInfoCacheByID.ContainsKey(userId))
                {
                    ai = m_UserInfoCacheByID[userId] as AccountInfo;
                    m_List.Remove(ai.ListNode);
                    m_List.AddLast(ai.ListNode);
                }
                else
                {
                    ai = RefreshUserInfo(userId);
                }
                return ai;
            }
        }

        /// <summary>
        /// 更新用户信息
        /// </summary>
        /// <param name="name"></param>
        /// <param name="values"></param>
        public void UpdateUserInfo(string name, Hashtable values)
        {
            lock (m_Lock)
            {
                m_IAccountStorage.UpdateUserInfo(name, values);
                RefreshUserInfo(name);
            }
        }

        /// <summary>
        /// 添加好友
        /// </summary>
        /// <param name="user"></param>
        /// <param name="friend"></param>
        /// <param name="index"></param>
        public void AddFriend(string user, string friend, int index)
        {
            lock (m_Lock)
            {
                if (m_IAccountStorage.GetRelationship(user, friend) == -1)
                {
                    AddFriend(user, friend);
                }
            }
        }

        public void AddFriend(string user, string friend)
        {
            lock (m_Lock)
            {
                AccountInfo userInfo = AccountImpl.Instance.GetUserInfo(user);
                AccountInfo friendInfo = AccountImpl.Instance.GetUserInfo(friend);

                if (String.Compare(user, friend, true) != 0 && !userInfo.ContainsFriend(friend))
                {
                    m_IAccountStorage.AddFriend(user, friend);

                    RefreshUserInfo(user);
                    RefreshUserInfo(friend);

                }
            }
        }

        /// <summary>
        /// 删除好友
        /// </summary>
        /// <param name="user"></param>
        /// <param name="friend"></param>
        public void DeleteFriend(string user, string friend)
        {
            lock (m_Lock)
            {
                if (m_IAccountStorage.GetRelationship(user, friend) != -1)
                {
                    AccountInfo userInfo = GetUserInfo(user);
                    AccountInfo friendInfo = GetUserInfo(friend);

                    m_IAccountStorage.DeleteFriend(userInfo.ID, friendInfo.ID);

                    RefreshUserInfo(user);
                    RefreshUserInfo(friend);

                }
            }
        }

        public void DeleteUser(String name)
        {
            AccountInfo info = GetUserInfo(name);
            Int64 id = info.ID;
            List<String> friends = new List<string>();
            foreach (string s in info.Friends) friends.Add(s);

			m_IAccountStorage.DeleteUser(info.ID);

			try
			{
				Core.IO.Directory.Rename(String.Format("/{0}", name), String.Format("/_del_{1}_{0}", name, id));
			}
			catch
			{
			}

            foreach (string friend in friends)
            {
                RefreshUserInfo(friend);
            }
        }

        /// <summary>
        /// 删除群
        /// </summary>
        /// <param name="name"></param>
        /// <param name="creator"></param>
        public void DeleteGroup(String name, String creator)
        {
            AccountInfo info = GetUserInfo(name);
			Int64 id = info.ID;
            List<String> members = new List<string>();
			foreach (string s in info.Friends) members.Add(s);

			try
			{
				Core.IO.Directory.Rename(String.Format("/{0}", name), String.Format("/_del_{1}_{0}", name, id));
			}
			catch
			{
			}

            lock (m_Lock)
            {
                m_IAccountStorage.DeleteGroup(info.ID);

                foreach (string member in members)
                {
                    RefreshUserInfo(member);
                }
            }
        }

        public void CreateUser(String name, String nickname, String password, String email)
        {
            lock (m_Lock)
            {

                m_IAccountStorage.CreateUser(name, nickname, password, email);

                RefreshUserInfo("public");
                RefreshUserInfo("admin");
            }
        }

        public void CreateTempUser(String name, String nickname)
        {
            lock (m_Lock)
            {

                m_IAccountStorage.CreateTempUser(name, nickname);

                RefreshUserInfo("public");
                RefreshUserInfo("admin");
            }
        }

        /// <summary>
        /// 创建群
        /// </summary>
        /// <param name="creator"></param>
        /// <param name="name"></param>
        /// <param name="nickname"></param>
        public void CreateGroup(String creator, String name, String nickname)
        {
            lock (m_Lock)
            {
                m_IAccountStorage.CreateGroup(creator, name, nickname);

                RefreshUserInfo(creator);
            }
        }
    }
    #endregion

    #region AccountInfo旧的类
    //public class AccountInfo : IRenderJson
    //{
    //    object m_Lock = new object();

    //    private LinkedListNode<AccountInfo> m_ListNode;

    //    public LinkedListNode<AccountInfo> ListNode
    //    {
    //        get { return m_ListNode; }
    //    }

    //    private String m_Name, m_NickName;
    //    private Int64 m_ID, m_Type;
    //    private String[] m_Roles;
    //    private FriendInfo[] m_Friends;
    //    private FriendInfo m_Creator;
    //    private String m_EMail, m_InviteCode;
    //    private Int64 m_DiskSize;
    //    private bool m_IsTemp;
    //    private String m_Password;

    //    private String m_WaitToDo, m_UnReadEmail,m_WaitToDoUrl,m_UnreadEmailUrl;// WaitToDo,UnReadEmail--- Elliot Add 2012-2-9 

    //    Hashtable m_FriendIndex = null;
    //    Hashtable m_Managers = null;

    //    bool m_AcceptStrangerIM;
    //    Int64 m_IMFileLimit, m_IMImageLimit;
    //    string m_HeadIMG;
    //    string m_Remark;

    //    DateTime m_RegisterTime = DateTime.Now;

    //    String m_HomePage = String.Empty;

    //    Details _detailsJson = null;

    //    public AccountInfo(
    //        String name, String nickname, Int64 id, Int64 type, String[] roles,
    //        FriendInfo[] friends, FriendInfo[] managers, FriendInfo creator,
    //        String email, String inviteCode,
    //        bool acceptStrangerIM, Int64 imf_limite, Int64 imimage_limit,
    //        Int64 diskSize, Int64 isTemp,
    //        DateTime registerTime,
    //        String homePage,
    //        string pwd,
    //        DataRow data,
    //        String waittodo,String unreademail,
    //        String waittodourl,String unreademailurl
    //    )
    //    {
    //        _detailsJson = new Details(this);
    //        m_ListNode = new LinkedListNode<AccountInfo>(this);
    //        Reset(name, nickname, id, type, roles, friends, managers, creator, email, inviteCode, acceptStrangerIM, imf_limite, imimage_limit, diskSize, isTemp, registerTime, homePage, pwd, data, waittodo, unreademail, waittodourl, unreademailurl);
    //    }

    //    public void Reset(
    //        String name, String nickname, Int64 id, Int64 type, String[] roles,
    //        FriendInfo[] friends, FriendInfo[] managers, FriendInfo creator,
    //        String email, String inviteCode,
    //        bool acceptStrangerIM, Int64 imf_limite, Int64 imimage_limit,
    //        Int64 diskSize, Int64 isTemp,
    //        DateTime registerTime,
    //        String homePage,
    //        string pwd,
    //        DataRow data,
    //        String waittodo,String unreademail,String waittodourl,String unreademailurl
    //    )
    //    {
    //        lock (m_Lock)
    //        {
    //            m_FriendIndex = new Hashtable();
    //            m_Managers = new Hashtable();
    //            m_Name = name;
    //            m_NickName = nickname;
    //            m_ID = id;
    //            m_Type = type;
    //            m_Roles = roles;
    //            m_Friends = friends;
    //            m_Creator = creator;
    //            m_EMail = email;
    //            m_InviteCode = inviteCode;
    //            m_HeadIMG = data["HeadIMG"].ToString();
    //            m_Remark = data["Remark"].ToString();

    //            m_Password = pwd;

    //            m_AcceptStrangerIM = acceptStrangerIM;
    //            m_IMFileLimit = imf_limite;
    //            m_IMImageLimit = imimage_limit;

    //            m_IsTemp = (isTemp != 0);

    //            m_HomePage = homePage;

    //            m_DiskSize = diskSize;

    //            foreach (FriendInfo friend in friends)
    //            {
    //                m_FriendIndex.Add(friend.Name.ToUpper(), friend);
    //            }

    //            if (managers != null)
    //            {
    //                foreach (FriendInfo friend in managers)
    //                {
    //                    m_Managers.Add(friend.Name.ToUpper(), friend);
    //                }
    //            }

    //            m_RegisterTime = registerTime;

    //            m_WaitToDo = waittodo;

    //            m_UnReadEmail = unreademail;

    //            m_WaitToDoUrl = waittodourl;

    //            m_UnreadEmailUrl = unreademailurl;
    //        }
    //    }

    //    public String[] Friends
    //    {
    //        get
    //        {
    //            lock (m_Friends)
    //            {
    //                String[] array = new String[m_Friends.Length];
    //                for (int i = 0; i < m_Friends.Length; i++) array[i] = m_Friends[i].Name;
    //                return array;
    //            }
    //        }
    //    }

    //    public String[] Roles
    //    {
    //        get
    //        {
    //            lock (m_Roles)
    //            {
    //                String[] array = new String[m_Roles.Length];
    //                m_Roles.CopyTo(array, 0);
    //                return array;
    //            }
    //        }
    //    }

    //    public Boolean IsTemp
    //    {
    //        get
    //        {
    //            return m_IsTemp;
    //        }
    //    }

    //    public String Creator
    //    {
    //        get
    //        {
    //            return m_Creator.Name;
    //        }
    //    }

    //    public String HeadIMG
    //    {
    //        get
    //        {
    //            return m_HeadIMG;
    //        }
    //    }

    //    public String Remark
    //    {
    //        get
    //        {
    //            return m_Remark;
    //        }
    //    }

    //    public String[] Groups
    //    {
    //        get
    //        {
    //            lock (m_Friends)
    //            {
    //                List<String> groups = new List<string>();
    //                foreach (FriendInfo friend in m_Friends)
    //                {
    //                    if (friend.PeerType == 1) groups.Add(friend.Name);
    //                }
    //                return groups.ToArray();
    //            }
    //        }
    //    }

    //    public Int64 Type
    //    {
    //        get
    //        {
    //            return m_Type;
    //        }
    //    }

    //    public bool AcceptStrangerIM
    //    {
    //        get
    //        {
    //            return m_AcceptStrangerIM;
    //        }
    //    }

    //    public Int64 MsgFileLimit
    //    {
    //        get
    //        {
    //            return m_IMFileLimit;
    //        }
    //    }

    //    public Int64 MsgImageLimit
    //    {
    //        get
    //        {
    //            return m_IMImageLimit;
    //        }
    //    }

    //    public String Nickname
    //    {
    //        get
    //        {
    //            return m_NickName;
    //        }
    //    }

    //    public String EMail
    //    {
    //        get
    //        {
    //            return m_EMail;
    //        }
    //    }

    //    public String Password
    //    {
    //        get
    //        {
    //            return m_Password;
    //        }
    //    }

    //    public String HomePage
    //    {
    //        get
    //        {
    //            return m_HomePage;
    //        }
    //    }

    //    public String Name
    //    {
    //        get
    //        {
    //            return m_Name;
    //        }
    //    }

    //    public Int64 DiskSize
    //    {
    //        get
    //        {
    //            if (Type == 1)
    //            {
    //                return AccountImpl.Instance.GetUserInfo(m_Creator.Name).DiskSize;
    //            }
    //            else
    //            {
    //                return m_DiskSize * 1024 * 1024;
    //            }
    //        }
    //    }

    //    public String InviteCode
    //    {
    //        get
    //        {
    //            return m_InviteCode;
    //        }
    //    }

    //    public Int64 ID
    //    {
    //        get
    //        {
    //            return m_ID;
    //        }
    //    }

    //    public DateTime RegisterTime
    //    {
    //        get
    //        {
    //            return m_RegisterTime;
    //        }
    //    }


    //    #region 待办，未读，WaitToDo,UnReadEmail--- Elliot Add 2012-2-9 
    //    public String WaitToDo
    //    {
    //        get
    //        {
    //            return m_WaitToDo;
    //        }
    //    }
    //    public String UnReadEmail
    //    {
    //        get
    //        {
    //            return m_UnReadEmail;
    //        }
    //    }

    //    public String WaitToDoUrl
    //    {
    //        get
    //        {
    //            return m_WaitToDoUrl;
    //        }
    //    }

    //    public String UnReadEmailUrl
    //    {
    //        get
    //        {
    //            return m_UnreadEmailUrl;
    //        }
    //    }
    //    #endregion 

    //    public bool IsRole(string role)
    //    {
    //        foreach (string s in m_Roles)
    //        {
    //            if (s == role) return true;
    //        }
    //        return false;
    //    }

    //    public bool ContainsFriend(string name)
    //    {
    //        return m_FriendIndex.ContainsKey(name.ToUpper());
    //    }

    //    public bool ContainsMember(string name)
    //    {
    //        return m_FriendIndex.ContainsKey(name.ToUpper());
    //    }

    //    public bool IsManagedBy(string name)
    //    {
    //        return Type == 1 && m_Managers.ContainsKey(name.ToUpper());
    //    }

    //    public bool IsCreatedBy(string name)
    //    {
    //        return Type == 1 && String.Compare(name, m_Creator.Name, true) == 0;
    //    }

    //    public DateTime GetGroupMemberRenewTime(string user)
    //    {
    //        string key = user.ToUpper();
    //        return (m_FriendIndex[key] as FriendInfo).RenewTime;
    //    }

    //    void IRenderJson.RenderJson(StringBuilder builder)
    //    {
    //        Utility.RenderHashJson(
    //            builder,
    //            "ID", m_ID,
    //            "Name", m_Name,
    //            "Nickname", m_NickName,
    //            "Type", m_Type
    //        );
    //    }

    //    public class Details : IRenderJson
    //    {
    //        AccountInfo _info;
    //        public Details(AccountInfo info)
    //        {
    //            _info = info;
    //        }

    //        void IRenderJson.RenderJson(StringBuilder builder)
    //        {
    //            bool online = SessionManagement.Instance.IsOnline(_info.Name);
    //            if (_info.Type == 0)
    //            {
    //                Utility.RenderHashJson(
    //                    builder,
    //                    "ID", _info.m_ID,
    //                    "Name", _info.m_Name,
    //                    "Nickname", _info.m_NickName,
    //                    "Type", _info.m_Type,
    //                    "EMail", _info.m_EMail,
    //                    "InviteCode", _info.m_InviteCode,
    //                    "HeadIMG", _info.m_HeadIMG,
    //                    "HomePage", _info.m_HomePage,
    //                    "Remark", _info.m_Remark,
    //                    "State", online ? "Online" : "Offline",
    //                    "IsTemp", _info.IsTemp,
    //                    "WaitToDo", _info.WaitToDo,
    //                    "UnReadEmail",_info.UnReadEmail,
    //                    "WaitToDoUrl",_info.WaitToDoUrl,
    //                    "UnReadEmailUrl",_info.UnReadEmailUrl
    //                );
    //            }
    //            else
    //            {
    //                Utility.RenderHashJson(
    //                    builder,
    //                    "ID", _info.m_ID,
    //                    "Name", _info.m_Name,
    //                    "Nickname", _info.m_NickName,
    //                    "Type", _info.m_Type,
    //                    "EMail", _info.m_EMail,
    //                    "InviteCode", _info.m_InviteCode,
    //                    "HeadIMG", _info.m_HeadIMG,
    //                    "HomePage", _info.m_HomePage,
    //                    "Remark", _info.m_Remark,
    //                    "IsTemp", _info.IsTemp,
    //                    "WaitToDo", _info.WaitToDo,
    //                    "UnReadEmail", _info.UnReadEmail,
    //                    "WaitToDoUrl", _info.WaitToDoUrl,
    //                    "UnReadEmailUrl", _info.UnReadEmailUrl
    //                );
    //            }
    //        }
    //    }

    //    public Details DetailsJson
    //    {
    //        get { return _detailsJson; }
    //    }
    //}
    #endregion

    #region AccountInfo新的类
    public class AccountInfo : IRenderJson
    {
        // Fields
        private Details _detailsJson;
        private bool m_AcceptStrangerIM;
        private FriendInfo m_Creator;
        private long m_DiskSize;
        private string m_DType;
        private string m_EMail;
        private Hashtable m_FriendIndex;
        private FriendInfo[] m_Friends;
        private string m_HeadIMG;
        private string m_HomePage;
        private long m_ID;
        private long m_IMFileLimit;
        private long m_IMImageLimit;
        private string m_InviteCode;
        private bool m_IsExitGroup;
        private bool m_IsTemp;
        private LinkedListNode<AccountInfo> m_ListNode;
        private object m_Lock;
        private Hashtable m_Managers;
        private string m_MobilePhone;
        private string m_Name;
        private string m_NickName;
        private string m_ParentId;
        private string m_Password;
        private string m_Phone;
        private DateTime m_RegisterTime;
        private string m_Remark;
        private string[] m_Roles;
        private string m_TelPhone;
        private long m_Type;

        private String m_WaitToDo, m_UnReadEmail, m_WaitToDoUrl, m_UnreadEmailUrl;// WaitToDo,UnReadEmail--- Elliot Add 2012-2-9 

        public AccountInfo(string name, string nickname, long id, long type, string[] roles, FriendInfo[] friends, FriendInfo[] managers, FriendInfo creator, string email, string inviteCode, bool acceptStrangerIM, long imf_limite, long imimage_limit, long diskSize, long isTemp, DateTime registerTime, string homePage, string pwd, DataRow data)
        {
            this.m_Lock = new object();
            this.m_FriendIndex = null;
            this.m_Managers = null;
            this.m_IsExitGroup = false;
            this.m_RegisterTime = DateTime.Now;
            this.m_HomePage = string.Empty;
            this._detailsJson = null;
            this._detailsJson = new Details(this);
            this.m_ListNode = new LinkedListNode<AccountInfo>(this);
            this.Reset(name, nickname, id, type, roles, friends, managers, creator, email, inviteCode, acceptStrangerIM, imf_limite, imimage_limit, diskSize, isTemp, registerTime, homePage, pwd, data);
        }

        #region 重载构造函数
        public AccountInfo(
            String name, String nickname, Int64 id, Int64 type, String[] roles,
            FriendInfo[] friends, FriendInfo[] managers, FriendInfo creator,
            String email, String inviteCode,
            bool acceptStrangerIM, Int64 imf_limite, Int64 imimage_limit,
            Int64 diskSize, Int64 isTemp,
            DateTime registerTime,
            String homePage,
            string pwd,
            DataRow data,
            String waittodo, String unreademail,
            String waittodourl, String unreademailurl
        )
        {
            this.m_Lock = new object();
            this.m_FriendIndex = null;
            this.m_Managers = null;
            this.m_IsExitGroup = false;
            this.m_RegisterTime = DateTime.Now;
            this.m_HomePage = string.Empty;
            this._detailsJson = null;
            _detailsJson = new Details(this);
            m_ListNode = new LinkedListNode<AccountInfo>(this);
            Reset(name, nickname, id, type, roles, friends, managers, creator, email, inviteCode, acceptStrangerIM, imf_limite, imimage_limit, diskSize, isTemp, registerTime, homePage, pwd, data, waittodo, unreademail, waittodourl, unreademailurl);
        }
        #endregion

        public bool ContainsFriend(string name)
        {
            lock (this.m_Lock)
            {
                return this.m_FriendIndex.ContainsKey(name.ToUpper());
            }
        }

        public bool ContainsMember(string name)
        {
            lock (this.m_Lock)
            {
                return this.m_FriendIndex.ContainsKey(name.ToUpper());
            }
        }

        void IRenderJson.RenderJson(StringBuilder builder)
        {
            Utility.RenderHashJson(builder, new object[] { "ID", this.m_ID, "Name", this.m_Name, "Nickname", this.m_NickName, "Type", this.m_Type });
        }

        public DateTime GetGroupMemberRenewTime(string user)
        {
            lock (this.m_Lock)
            {
                string key = user.ToUpper();
                return (this.m_FriendIndex[key] as FriendInfo).RenewTime;
            }
        }
        public bool IsCreatedBy(string name)
        {
            lock (this.m_Lock)
            {
                return ((this.Type == 1L) && (string.Compare(name, this.m_Creator.Name, true) == 0));
            }
        }

        public bool IsManagedBy(string name)
        {
            lock (this.m_Lock)
            {
                return ((this.Type == 1L) && this.m_Managers.ContainsKey(name.ToUpper()));
            }
        }
        public bool IsRole(string role)
        {
            lock (this.m_Lock)
            {
                foreach (string s in this.Roles)
                {
                    if (s == role)
                    {
                        return true;
                    }
                }
                return false;
            }
        }

        #region Reset() 23个参数
        public void Reset(
            String name, String nickname, Int64 id, Int64 type, String[] roles,
            FriendInfo[] friends, FriendInfo[] managers, FriendInfo creator,
            String email, String inviteCode,
            bool acceptStrangerIM, Int64 imf_limite, Int64 imimage_limit,
            Int64 diskSize, Int64 isTemp,
            DateTime registerTime,
            String homePage,
            string pwd,
            DataRow data,
            String waittodo, String unreademail, String waittodourl, String unreademailurl
        )
        {
            lock (m_Lock)
            {
                m_FriendIndex = new Hashtable();
                m_Managers = new Hashtable();
                m_Name = name;
                m_NickName = nickname;
                m_ID = id;
                m_Type = type;
                m_Roles = roles;
                m_Friends = friends;
                m_Creator = creator;
                m_EMail = email;
                m_InviteCode = inviteCode;
                m_HeadIMG = data["HeadIMG"].ToString();
                m_Remark = data["Remark"].ToString();

                m_Password = pwd;

                m_AcceptStrangerIM = acceptStrangerIM;
                m_IMFileLimit = imf_limite;
                m_IMImageLimit = imimage_limit;

                m_IsTemp = (isTemp != 0);

                m_HomePage = homePage;

                m_DiskSize = diskSize;

                foreach (FriendInfo friend in friends)
                {
                    try
                    {
                        m_FriendIndex.Add(friend.Name.ToUpper(), friend);
                    }
                    catch
                    { }
                }

                if (managers != null)
                {
                    foreach (FriendInfo friend in managers)
                    {
                        try
                        {
                            m_Managers.Add(friend.Name.ToUpper(), friend);
                        }
                        catch
                        { }
                    }
                }

                m_RegisterTime = registerTime;

                m_WaitToDo = waittodo;

                m_UnReadEmail = unreademail;

                m_WaitToDoUrl = waittodourl;

                m_UnreadEmailUrl = unreademailurl;
            }
        }
        #endregion

        public void Reset(string name, string nickname, long id, long type, string[] roles, FriendInfo[] friends, FriendInfo[] managers, FriendInfo creator, string email, string inviteCode, bool acceptStrangerIM, long imf_limite, long imimage_limit, long diskSize, long isTemp, DateTime registerTime, string homePage, string pwd, DataRow data)
        {
            lock (this.m_Lock)
            {
                this.m_FriendIndex = new Hashtable();
                this.m_Managers = new Hashtable();
                this.m_Name = name;
                this.m_NickName = nickname;
                this.m_ID = id;
                this.m_Type = type;
                this.m_Roles = roles;
                this.m_Friends = friends;
                this.m_Creator = creator;
                this.m_EMail = email;
                this.m_InviteCode = inviteCode;
                if (data["HeadIMG"] != null)
                {
                    this.m_HeadIMG = data["HeadIMG"].ToString();
                }
                else
                {
                    this.m_HeadIMG = "";
                }
                if (data["Remark"] != null)
                {
                    this.m_Remark = data["Remark"].ToString();
                }
                else
                {
                    this.m_Remark = "这个人很懒,什么都没留下.";
                }
                if (data.Table.Columns.Contains("ParentId") && (data["ParentId"] != null))
                {
                    this.m_ParentId = data["ParentId"].ToString();
                }
                if (data.Table.Columns.Contains("DType") && (data["DType"] != null))
                {
                    this.m_DType = data["DType"].ToString();
                }
                this.m_Password = pwd;
                if (data.Table.Columns.Contains("Phone") && (data["Phone"] != null))
                {
                    this.m_Phone = data["Phone"].ToString();
                }
                if (data.Table.Columns.Contains("TelPhone") && (data["TelPhone"] != null))
                {
                    this.m_TelPhone = data["TelPhone"].ToString();
                }
                if (data.Table.Columns.Contains("MobilePhone") && (data["MobilePhone"] != null))
                {
                    this.m_MobilePhone = data["MobilePhone"].ToString();
                }
                if (data.Table.Columns.Contains("IsExitGroup") && (data["IsExitGroup"] != null))
                {
                    this.m_IsExitGroup = Convert.ToInt64(data["IsExitGroup"]) == 1L;
                }
                this.m_AcceptStrangerIM = acceptStrangerIM;
                this.m_IMFileLimit = imf_limite;
                this.m_IMImageLimit = imimage_limit;
                this.m_IsTemp = isTemp != 0L;
                this.m_HomePage = homePage;
                this.m_DiskSize = diskSize;
                if (friends != null)
                {
                    foreach (FriendInfo friend in friends)
                    {
                        this.m_FriendIndex.Add(friend.Name.ToUpper(), friend);
                    }
                }
                if (managers != null)
                {
                    foreach (FriendInfo friend in managers)
                    {
                        this.m_Managers.Add(friend.Name.ToUpper(), friend);
                    }
                }
                this.m_RegisterTime = registerTime;
            }
        }
        public bool AcceptStrangerIM
        {
            get
            {
                lock (this.m_Lock)
                {
                    return this.m_AcceptStrangerIM;
                }
            }
        }

        public string Creator
        {
            get
            {
                lock (this.m_Lock)
                {
                    string name = string.Empty;
                    if (this.m_Creator != null)
                    {
                        name = this.m_Creator.Name;
                    }
                    return name;
                }
            }
        }

        public Details DetailsJson
        {
            get
            {
                return this._detailsJson;
            }
        }

        public long DiskSize
        {
            get
            {
                if (this.Type == 1L)
                {
                    return AccountImpl.Instance.GetUserInfo(this.m_Creator.Name).DiskSize;
                }
                lock (this.m_Lock)
                {
                    return ((this.m_DiskSize * 0x400L) * 0x400L);
                }
            }
        }

        public string DType
        {
            get
            {
                lock (this.m_Lock)
                {
                    return this.m_DType;
                }
            }
        }

        public string EMail
        {
            get
            {
                lock (this.m_Lock)
                {
                    return this.m_EMail;
                }
            }
        }

        public string[] Friends
        {
            get
            {
                lock (this.m_Lock)
                {
                    string[] array = new string[this.m_Friends.Length];
                    for (int i = 0; i < this.m_Friends.Length; i++)
                    {
                        array[i] = this.m_Friends[i].Name;
                    }
                    return array;
                }
            }
        }
        public string[] Groups
        {
            get
            {
                lock (this.m_Lock)
                {
                    List<string> groups = new List<string>();
                    foreach (FriendInfo friend in this.m_Friends)
                    {
                        if (friend.PeerType == 1L)
                        {
                            groups.Add(friend.Name);
                        }
                    }
                    return groups.ToArray();
                }
            }
        }
        public string HeadIMG
        {
            get
            {
                lock (this.m_Lock)
                {
                    return this.m_HeadIMG;
                }
            }
        }
        public string HomePage
        {
            get
            {
                lock (this.m_Lock)
                {
                    return this.m_HomePage;
                }
            }
        }
        public long ID
        {
            get
            {
                lock (this.m_Lock)
                {
                    return this.m_ID;
                }
            }
        }

        public string InviteCode
        {
            get
            {
                lock (this.m_Lock)
                {
                    return this.m_InviteCode;
                }
            }
        }
        public bool IsExitGroup
        {
            get
            {
                lock (this.m_Lock)
                {
                    return this.m_IsExitGroup;
                }
            }
        }

        public bool IsTemp
        {
            get
            {
                lock (this.m_Lock)
                {
                    return this.m_IsTemp;
                }
            }
        }

        public LinkedListNode<AccountInfo> ListNode
        {
            get
            {
                return this.m_ListNode;
            }
        }
        public string MobilePhone
        {
            get
            {
                lock (this.m_Lock)
                {
                    return this.m_MobilePhone;
                }
            }
        }
        public long MsgFileLimit
        {
            get
            {
                lock (this.m_Lock)
                {
                    return this.m_IMFileLimit;
                }
            }
        }
        public long MsgImageLimit
        {
            get
            {
                lock (this.m_Lock)
                {
                    return this.m_IMImageLimit;
                }
            }
        }
        public string Name
        {
            get
            {
                lock (this.m_Lock)
                {
                    return this.m_Name;
                }
            }
        }

        public string Nickname
        {
            get
            {
                lock (this.m_Lock)
                {
                    return this.m_NickName;
                }
            }
        }

        public string ParentId
        {
            get
            {
                lock (this.m_Lock)
                {
                    return this.m_ParentId;
                }
            }
        }

        public string Password
        {
            get
            {
                lock (this.m_Lock)
                {
                    return this.m_Password;
                }
            }
        }
        public string Phone
        {
            get
            {
                lock (this.m_Lock)
                {
                    return this.m_Phone;
                }
            }
        }
        public DateTime RegisterTime
        {
            get
            {
                lock (this.m_Lock)
                {
                    return this.m_RegisterTime;
                }
            }
        }
        public string Remark
        {
            get
            {
                lock (this.m_Lock)
                {
                    return this.m_Remark;
                }
            }
        }
        public string[] Roles
        {
            get
            {
                lock (this.m_Lock)
                {
                    string[] array = new string[this.m_Roles.Length];
                    this.m_Roles.CopyTo(array, 0);
                    return array;
                }
            }
        }
        public string TelPhone
        {
            get
            {
                lock (this.m_Lock)
                {
                    return this.m_TelPhone;
                }
            }
        }

        public long Type
        {
            get
            {
                lock (this.m_Lock)
                {
                    return this.m_Type;
                }
            }
        }

        #region 待办，未读，WaitToDo,UnReadEmail--- Elliot Add 2012-2-9
        public String WaitToDo
        {
            get
            {
                return m_WaitToDo;
            }
        }
        public String UnReadEmail
        {
            get
            {
                return m_UnReadEmail;
            }
        }

        public String WaitToDoUrl
        {
            get
            {
                return m_WaitToDoUrl;
            }
        }

        public String UnReadEmailUrl
        {
            get
            {
                return m_UnreadEmailUrl;
            }
        }
        #endregion 

        public class Details : IRenderJson
        {
            // Fields
            private AccountInfo _info;

            // Methods
            public Details(AccountInfo info)
            {
                this._info = info;
            }

            
                        
                        
                        

            void IRenderJson.RenderJson(StringBuilder builder)
            {
                bool online = SessionManagement.Instance.IsOnline(this._info.Name);
                if (this._info.Type == 0L)
                {
                    Utility.RenderHashJson(builder, new object[] { 
                "ID", this._info.m_ID, "Name", this._info.m_Name, "Nickname", this._info.m_NickName, "Type", this._info.m_Type, "EMail", this._info.m_EMail, "InviteCode", this._info.m_InviteCode, "HeadIMG", this._info.m_HeadIMG, "HomePage", this._info.m_HomePage, 
                "Remark", this._info.m_Remark, "ParentId", this._info.m_ParentId, "DType", this._info.m_DType, "State", online ? "Online" : "Offline", "GroupCreator", "", "Phone", this._info.Phone, "TelPhone", this._info.TelPhone, "MobilePhone", this._info.MobilePhone, 
                "IsExitGroup", this._info.IsExitGroup,"WaitToDo", _info.WaitToDo,"UnReadEmail", _info.UnReadEmail,"WaitToDoUrl", _info.WaitToDoUrl,"UnReadEmailUrl", _info.UnReadEmailUrl
             });
                }
                else
                {
                    Utility.RenderHashJson(builder, new object[] { 
                "ID", this._info.m_ID, "Name", this._info.m_Name, "Nickname", this._info.m_NickName, "Type", this._info.m_Type, "EMail", this._info.m_EMail, "InviteCode", this._info.m_InviteCode, "HeadIMG", this._info.m_HeadIMG, "HomePage", this._info.m_HomePage, 
                "Remark", this._info.m_Remark, "ParentId", this._info.m_ParentId, "DType", this._info.m_DType, "State", online ? "Online" : "Offline", "GroupCreator", this._info.Creator, "Phone", this._info.Phone, "TelPhone", this._info.TelPhone, "MobilePhone", this._info.MobilePhone, 
                "IsExitGroup", this._info.IsExitGroup,"WaitToDo", _info.WaitToDo,"UnReadEmail", _info.UnReadEmail,"WaitToDoUrl", _info.WaitToDoUrl,"UnReadEmailUrl", _info.UnReadEmailUrl
             });
                }
            }
        }
    }
    #endregion

    #region Friendinfo类
    public class FriendInfo
    {
        public String Name;
        public DateTime RenewTime;
        public Int64 Relationthip;
        public Int64 PeerType;

        public FriendInfo(string name, DateTime renewTime, Int64 relationthip, Int64 peerType)
        {
            Name = name;
            RenewTime = renewTime;
            Relationthip = relationthip;
            PeerType = peerType;
        }
    }
    #endregion
}
